<template>
	<view class="m-input">
		<view class="m-input-input">
			<input :focus="focus_" :type="inputType" :value="value" @input="onInput" class="input-input" :placeholder="placeholder"
			 :password="type==='password'&&!showPassword" @focus="onFocus" @blur="onBlur" :maxlength="maxLength" :disabled="disabled" />
		</view>
		<!-- 优先显示密码可见按钮 -->
		<!-- <view v-if="clearable_&&!displayable_&& value && value.length" class="m-input-icon">
            <ep-icon color="#cccccc" type="close" size="40" @click="clear"></ep-icon>
        </view> -->
		<view class="m-input-icon">
			<view class="micon iconfont icon-yanjing" v-if="displayable_" :style="{color:showPassword?'#666666':'#cccccc'}"
			 @click="display"></view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		props: {
			/**
			 * 输入类型
			 */
			type: String,
			/**
			 * 值
			 */
			value: String,
			/**
			 * 占位符
			 */
			placeholder: String,
			/**
			 * 是否显示清除按钮
			 */
			clearable: {
				type: [Boolean, String],
				default: false
			},
			/**
			 * 是否显示密码可见按钮
			 */
			displayable: {
				type: [Boolean, String],
				default: false
			},
			/**
			 * 自动获取焦点
			 */
			focus: {
				type: [Boolean, String],
				default: false
			},
			/*
			 * maxLength
			 */
			maxLength: {
				type: Number,
				default: 1000
			},
			/* 
			 * 是否禁用
			 */
			disabled: {
				type: Boolean,
				default: false
			}
		},
		model: {
			prop: 'value',
			event: 'input'
		},
		data() {
			return {
				/**
				 * 显示密码明文
				 */
				showPassword: false,
				/**
				 * 是否获取焦点
				 */
				isFocus: false
			}
		},
		computed: {
			inputType() {
				const type = this.type
				return type === 'password' ? 'text' : type
			},
			clearable_() {
				return String(this.clearable) !== 'false'
			},
			displayable_() {
				return String(this.displayable) !== 'false'
			},
			focus_() {
				return String(this.focus) !== 'false'
			}
		},
		methods: {
			clear() {
				// this.value = ''
				this.$emit("input", "")
			},
			display() {
				this.showPassword = !this.showPassword
			},
			onFocus() {
				this.isFocus = true
			},
			onBlur(e) {
				this.$nextTick(() => {
					this.isFocus = false
					this.$emit("onblur", e)
				})
			},
			onInput(e) {
				this.$emit('input', e.target.value)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.m-input {
		display: flex;
		flex: 1;
		flex-direction: row;

		.m-input-input {
			padding: 5rpx 15rpx;
			display: flex;
			flex: 15;

			.input-input {
				font-size: 28rpx;
				line-height: 50rpx;
				height: 50rpx;
				flex: 1;
			}
			.uni-input-placeholder{
				font-size: 28rpx;
				color: #989898;
			}
		}

		.m-input-icon {
			display: flex;
			flex-direction: row-reverse;
			flex: 1;

			.micon {
				width: 32rpx;
				height: 32rpx;
				padding: 10rpx 20rpx 10rpx 50rpx;
			}
		}
	}
</style>
